﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XWG.JWBST.ProcessInterfaces;
using System.IO;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using XWG.Framework.AuthUtility;
using System.Collections.Specialized;
using System.Web.Script.Serialization;

namespace XWG.JWBST.KckpInfoProcess
{
    /// <summary>
    /// 快处快赔基本信息上传
    /// </summary>
    public class Process : IProcess
    {
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            string info = reader.ReadToEnd();
            info = HttpUtility.UrlDecode(info);
            JObject oka = (JObject)JsonConvert.DeserializeObject(info);

            string processType = oka["Processtype"].ToString().Replace("\"", string.Empty);

            if (processType == "GetLSH")
            {
                string lshnum = XWG.JWBST.JWBSTBLL.TbBstFastprocessBLL.GetLSH("TB_BST_FASTPROCESS");
                return lshnum;
            }
            else if (processType == "InsertInfo")
            {
                XWG.JWBST.JWBSTEntity.TbBstFastprocess accidentInfo = new JWBSTEntity.TbBstFastprocess();
                Dictionary<string, object> dic = JsonToDictionary(info);//将Json数据转成dictionary格式
                Dictionary<string, object> dataSet = (Dictionary<string, object>)dic;
                ////使用KeyValuePair遍历数据
                ////数据来源判断
                //foreach (KeyValuePair<string, object> item in dataSet)
                //{
                //    if (item.Key.ToString() == "Accident")
                //    {
                //        accidentInfo.FastMark = "1";//数据来源 1为其他软件提交 后期如有需求可加入字典表
                //        break;
                //    }
                //    else
                //    {
                //        //数据来源  0为济南民生警务手机端提交
                //        accidentInfo.FastMark = "0";
                //    }
                //}
                //IOS判断
                foreach (KeyValuePair<string, object> item in dataSet)
                {
                    if (item.Key.ToString() == "DeviceToken")//获取header数据
                    {
                        accidentInfo.FastToken = oka["DeviceToken"].ToString().Replace("\"", string.Empty);
                        break;
                    }
                    else
                    {
                        accidentInfo.FastToken = string.Empty;
                    }
                }

                accidentInfo.FastId = Guid.NewGuid().ToString();
                accidentInfo.FastLsh = oka["Lsh"].ToString().Replace("\"", string.Empty);
                accidentInfo.FastAddress = oka["Address"].ToString().Replace("\"", string.Empty);
                accidentInfo.FastUptime = DateTime.Now;
                accidentInfo.FastAuditstatus = "0";
                accidentInfo.FastDel = "0";
                accidentInfo.FastComplete = "0";
                accidentInfo.FastIspolice = "0";
                accidentInfo.FastMark = "0"; //数据来源  0为济南民生警务手机端提交
                ResultModel re = new ResultModel();

                re = XWG.JWBST.JWBSTBLL.TbBstFastprocessBLL.Insert(accidentInfo, "", false);
                if (re.ISSucceed)
                {
                    return re.FiledKey;
                }
                else
                {
                    return "操作失败！";
                }
            }
            else
            {
                ResultModel re = new ResultModel();

                string FastId = oka["ID"].ToString().Replace("\"", string.Empty);
                var GetInfo = XWG.JWBST.JWBSTBLL.TbBstFastprocessBLL.Load(FastId);
                GetInfo.FastId = FastId;
                GetInfo.FastPhonea = oka["Phone_A"].ToString().Replace("\"", string.Empty);
                if (string.IsNullOrEmpty(oka["Phone_B"].ToString().Replace("\"", string.Empty)))
                {

                }
                else
                {
                    GetInfo.FastPhoneb = oka["Phone_B"].ToString().Replace("\"", string.Empty);
                }
                GetInfo.FastLpna = oka["CarMine"].ToString().Replace("\"", string.Empty);
                GetInfo.FastLpnb = oka["CarOther"].ToString().Replace("\"", string.Empty);
                GetInfo.FastGpsJd = oka["GPS_JD"].ToString().Replace("\"", string.Empty);
                GetInfo.FastGpsWd = oka["GPS_WD"].ToString().Replace("\"", string.Empty);
                GetInfo.FastComplete = "1";
                //记录请求信息
                JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
                visit.Id = Guid.NewGuid().ToString();
                visit.Phone = GetInfo.FastPhonea;
                visit.Time = DateTime.Now;
                visit.Modulename = "Kckp";
                visit.Optionname = "快处快赔基本信息上传";
                visit.Other = oka.ToString();
                JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);
                re = XWG.JWBST.JWBSTBLL.TbBstFastprocessBLL.Update(GetInfo, "", false);
                return re.Message;
            }
        }

        /// <summary>
        /// 将json数据反序列化为Dictionary
        /// </summary>
        /// <param name="jsonData">json数据</param>
        /// <returns></returns>
        private Dictionary<string, object> JsonToDictionary(string jsonData)
        {
            //实例化JavaScriptSerializer类的新实例
            JavaScriptSerializer jss = new JavaScriptSerializer();
            try
            {
                //将指定的 JSON 字符串转换为 Dictionary<string, object> 类型的对象
                return jss.Deserialize<Dictionary<string, object>>(jsonData);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
